//
//  YMTestReportTopInforView.m
//  YMMedical
//
//  Created by y s on 2023/7/21.
//

#import "YMTestReportTopInforView.h"

@interface YMTestReportTopInforView ()

@property (nonatomic, strong) UIImageView *imgvBg;
@property (nonatomic, strong) UILabel *lblItemBox;
@property (nonatomic, strong) UILabel *lblItem;

@property (nonatomic, strong) UIView *vValue;
@property (nonatomic, strong) UILabel *lblUnit;
@property (nonatomic, strong) UILabel *lblValue;

@property (nonatomic, strong) UIView *vConsult;
@property (nonatomic, strong) UILabel *lblConsult;
@property (nonatomic, strong) UILabel *lblConUnit;

@property (nonatomic, strong) UIView *vBox;
@property (nonatomic, strong) UILabel *lblType;
@property (nonatomic, strong) UILabel *lblResult;
@property (nonatomic, strong) UILabel *lblTime;

@end

@implementation YMTestReportTopInforView

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    [self addSubview:self.imgvBg];
    [self.imgvBg addSubview:self.lblItemBox];
    [self.imgvBg addSubview:self.lblItem];
    
    [self.imgvBg addSubview:self.vValue];
    [self.vValue addSubview:self.lblValue];
    [self.vValue addSubview:self.lblUnit];
    
    [self.imgvBg addSubview:self.vConsult];
    [self.vConsult addSubview:self.lblConsult];
    [self.vConsult addSubview:self.lblConUnit];
    
    [self addSubview:self.vBox];
    [self.vBox addSubview:self.lblResult];
    [self.vBox addSubview:self.lblName];
    [self.vBox addSubview:self.lblYear];
    [self.vBox addSubview:self.lblType];
    [self.vBox addSubview:self.lblTime];
    [self.vBox addSubview:self.lblWarning];
    
    [self.imgvBg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(0);
        make.height.mas_equalTo([SFitUtil fit:222]);
    }];
    [self.lblItemBox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:40]);
        make.right.mas_equalTo([SFitUtil fit:-40]);
        make.height.mas_equalTo([SFitUtil fit:48]);
        make.top.mas_equalTo(0);
    }];
    [self.lblItem mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lblItemBox.mas_bottom).offset([SFitUtil fit:18]);
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
    }];
    [self.vValue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.left.mas_greaterThanOrEqualTo([SFitUtil fit:15]);
        make.top.equalTo(self.lblItem.mas_bottom).offset([SFitUtil fit:12]);
    }];
    [self.lblValue mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
    }];
    [self.lblUnit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.top.mas_greaterThanOrEqualTo(0);
        make.centerY.mas_equalTo(0);
        make.left.equalTo(self.lblValue.mas_right).offset([SFitUtil fit:10]);
    }];
    [self.vConsult mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.left.mas_greaterThanOrEqualTo([SFitUtil fit:15]);
        make.top.equalTo(self.vValue.mas_bottom).offset([SFitUtil fit:12]);
    }];
    [self.lblConsult mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
    }];
    [self.lblConUnit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.top.mas_greaterThanOrEqualTo(0);
        make.centerY.mas_equalTo(0);
        make.left.equalTo(self.lblConsult.mas_right).offset([SFitUtil fit:10]);
    }];
    
    [self.vBox mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.top.mas_equalTo([SFitUtil fit:176]);
        make.left.mas_equalTo([SFitUtil fit:15]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
    }];
    [self.lblResult mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([SFitUtil fit:16]);
        make.right.mas_equalTo([SFitUtil fit:-15]);
        make.left.mas_equalTo([SFitUtil fit:15]);
    }];
    [self.lblName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([SFitUtil fit:52]);
        make.left.equalTo(self.lblResult);
        make.right.equalTo(self.lblYear.mas_left).offset([SFitUtil fit:-5]);
    }];
    [self.lblYear mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lblName);
        make.right.equalTo(self.lblResult);
        make.left.mas_equalTo([SFitUtil fit:175]);
    }];
    [self.lblName setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];
    [self.lblYear setContentCompressionResistancePriority:UILayoutPriorityDefaultHigh forAxis:UILayoutConstraintAxisHorizontal];
    
    [self.lblType mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([SFitUtil fit:52+32]);
        make.left.equalTo(self.lblName);
        make.right.equalTo(self.lblTime.mas_left).offset([SFitUtil fit:-3]);
    }];
    [self.lblTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.lblType);
        make.right.equalTo(self.lblResult);
        make.left.equalTo(self.lblYear);
    }];
    [self.lblWarning mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([SFitUtil fit:116]);
        make.left.right.equalTo(self.lblResult);
        make.bottom.mas_equalTo([SFitUtil fit:-0]);
    }];
}
- (void)setDict:(NSDictionary *)dict{
    _dict = dict;
    
    NSString *minorTermName = [SDataUtil stringOf:dict[@"minorTermName"] valid:@""];
    self.lblItemBox.text = [NSString stringWithFormat:@"%@%@",minorTermName,KLanguage(@"dete_report")];
    self.lblItem.text = [NSString stringWithFormat:@"%@（%@）",minorTermName,[SDataUtil stringOf:dict[@"minorTermEnglishName"] valid:@""]];
    
    self.lblValue.text = [SDataUtil stringOf:dict[@"adjustedDisplayResult"] valid:@""];
    self.lblUnit.text = [SDataUtil stringOf:dict[@"adjustedUnit"] valid:@""];
    NSString *sampleType = [SDataUtil stringOf:dict[@"sampleType"] valid:@""];
    self.lblType.text = [NSString stringWithFormat:@"%@%@",KLanguage(@"sample_format_txt"),sampleType];
    if (([sampleType isEqualToString:KLanguage(@"niao_24")] && [minorTermName isEqualToString:KLanguage(@"urinary_microalbumin_txt")])||
        [minorTermName isEqualToString:KLanguage(@"whole_process_c_reactive_protein_txt")]){
        self.lblConsult.text = [SDataUtil stringOf:dict[@"primaryDisplayResult"] valid:@""];
        self.lblConUnit.text = [SDataUtil stringOf:dict[@"primaryUnit"]  valid:@""];
    }else{
        self.lblConsult.text = @" ";
        self.lblConUnit.text = @" ";
    }
    self.lblName.text = [NSString stringWithFormat:@"%@%@",KLanguage(@"test_fix"),[SDataUtil stringOf:dict[@"detectionUserName"] valid:@""]];
    self.lblYear.text = [NSString stringWithFormat:@"%@%@",KLanguage(@"age_fix"),[SDataUtil stringOf:dict[@"detectionUserAge"] valid:@""]];
    self.lblTime.text = [NSString stringWithFormat:@"%@%@",KLanguage(@"cai_time_fix"),[SDataUtil stringOf:dict[@"collectDate"] valid:@""]];
    self.lblResult.text = [SDataUtil stringOf:dict[@"minorTermAndResultDisplay"] valid:@""];
    NSString *warningMsg = [SDataUtil stringOf:dict[@"warningMsg"] valid:@""];
    if (warningMsg != nil && warningMsg.length > 0){
        self.lblWarning.text = warningMsg;
    }else{
        self.lblWarning.text = @"";
    }
    [self.lblWarning mas_updateConstraints:^(MASConstraintMaker *make) {
        if (warningMsg != nil && warningMsg.length > 0){
            make.bottom.mas_equalTo([SFitUtil fit:-16]);
        }else{
            make.bottom.mas_equalTo([SFitUtil fit:-0]);
        }
    }];
}

- (void)longPressAction:(UILongPressGestureRecognizer *)longPress{
    if (longPress.state == UIGestureRecognizerStateBegan) {
        //手势开始的时候
        self.lblUnit.hidden = !_lblUnit.isHidden;
        if (_lblUnit.isHidden){
            self.lblValue.text = [SDataUtil stringOf:_dict[@"tcRatio"] valid:@""];
        }else{
            self.lblValue.text = [SDataUtil stringOf:_dict[@"adjustedDisplayResult"] valid:@""];
        }
    }else if (longPress.state == UIGestureRecognizerStateEnded) {
        
    }
}

#pragma mark -- setter and getter
- (UIImageView *)imgvBg{
    if (_imgvBg == nil){
        _imgvBg = [[UIImageView alloc] init];
        _imgvBg.image = [UIImage imageNamed:@"home_health_detail_top_bgImage"];
        _imgvBg.userInteractionEnabled = YES;
    }
    return _imgvBg;
}
- (UILabel *)lblItemBox{
    if (_lblItemBox == nil){
        _lblItemBox = [SThemeUtil boldLabel:@"" color:0xffffff fontSize:18];
        _lblItemBox.textAlignment = NSTextAlignmentCenter;
        _lblItemBox.adjustsFontSizeToFitWidth = YES;
        _lblItemBox.minimumScaleFactor = 0.6;
        
        CAShapeLayer *layer = [CAShapeLayer layer];
        layer.backgroundColor = STheme.D9.CGColor;
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, SCREEN_WIDTH-[SFitUtil fit:80], [SFitUtil fit:48]) byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake([SFitUtil fit:25], [SFitUtil fit:25])];
        layer.path = path.CGPath;
        _lblItemBox.layer.mask = layer;
        _lblItemBox.backgroundColor = [SUIUtil colorOf:0xffffff alpha:0.4];
    }
    return _lblItemBox;
}
- (UILabel *)lblItem{
    if (_lblItem == nil){
        _lblItem = [SThemeUtil boldLabel:@"" color:STheme.H_D9 fontSize:18];
        _lblItem.textAlignment = NSTextAlignmentCenter;
        _lblItem.adjustsFontSizeToFitWidth = YES;
        _lblItem.minimumScaleFactor = 0.6;
    }
    return _lblItem;
}
- (UIView *)vValue{
    if (_vValue == nil){
        _vValue = [[UIView alloc] init];
        _vValue.userInteractionEnabled = YES;
        UILongPressGestureRecognizer *tapLong = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
        [_vValue addGestureRecognizer:tapLong];
    }
    return _vValue;
}
- (UILabel *)lblValue{
    if (_lblValue == nil){
        _lblValue = [SThemeUtil boldLabel:@"" color:STheme.H_D9 fontSize:35];
        _lblValue.font = [SFitUtil fitFont:35 name:@"DingTalk Sans"];
        _lblValue.textAlignment = NSTextAlignmentRight;
        _lblValue.adjustsFontSizeToFitWidth = YES;
        _lblValue.minimumScaleFactor = 0.6;
    }
    return _lblValue;
}
- (UILabel *)lblUnit{
    if (_lblUnit == nil){
        _lblUnit = [SThemeUtil label:@"" color:STheme.H_D9 fontSize:16];
    }
    return _lblUnit;
}
- (UIView *)vConsult{
    if (_vConsult == nil){
        _vConsult = [[UIView alloc] init];
    }
    return _vConsult;
}
- (UILabel *)lblConsult{
    if (_lblConsult == nil){
        _lblConsult = [SThemeUtil boldLabel:@"" color:STheme.H_D9 fontSize:15];
        _lblConsult.textAlignment = NSTextAlignmentRight;
        _lblConsult.font = [SFitUtil fitFont:15 name:@"DingTalk Sans"];
    }
    return _lblConsult;
}
- (UILabel *)lblConUnit{
    if (_lblConUnit == nil){
        _lblConUnit = [SThemeUtil label:@"" color:STheme.H_D9 fontSize:12];
    }
    return _lblConUnit;
}
- (UIView *)vBox{
    if (_vBox == nil){
        _vBox = [[UIView alloc] init];
        _vBox.layer.cornerRadius = [SFitUtil fit:8];
        _vBox.clipsToBounds = YES;
        _vBox.backgroundColor = STheme.D9;
    }
    return _vBox;
}
- (UILabel *)lblName{
    if (_lblName == nil){
        _lblName = [SThemeUtil label:KLanguage(@"test_fix") color:STheme.H_D1 fontSize:14];
    }
    return _lblName;
}
- (UILabel *)lblResult{
    if (_lblResult == nil){
        _lblResult = [SThemeUtil boldLabel:@"--" color:0xFF3141 fontSize:14];
    }
    return _lblResult;
}
- (UILabel *)lblYear{
    if (_lblYear == nil){
        _lblYear = [SThemeUtil label:KLanguage(@"age_fix") color:STheme.H_D1 fontSize:14];
    }
    return _lblYear;
}
- (UILabel *)lblTime{
    if (_lblTime == nil){
        _lblTime = [SThemeUtil label:KLanguage(@"cai_time_fix") color:STheme.H_D1 fontSize:14];
    }
    return _lblTime;
}
- (UILabel *)lblType{
    if (_lblType == nil){
        _lblType = [SThemeUtil label:KLanguage(@"sample_format_txt") color:STheme.H_D1 fontSize:14];
    }
    return _lblType;
}
- (UILabel *)lblWarning{
    if (_lblWarning == nil){
        _lblWarning = [SThemeUtil boldLabel:@"" color:0xFF3141 fontSize:14];
        _lblWarning.numberOfLines = 0;
    }
    return _lblWarning;
}




@end
